﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;
using NetTopologySuite.Geometries.Utilities;
using NetTopologySuite.Noding;

namespace NetTopologySuite.Geometries.Prepared
{
	internal class PreparedLineStringIntersects
	{
		public static bool Intersects(PreparedLineString prep, IGeometry geom)
		{
			return new PreparedLineStringIntersects(prep).Intersects(geom);
		}

		public PreparedLineStringIntersects(PreparedLineString prepLine)
		{
			this.prepLine = prepLine;
		}

		public bool Intersects(IGeometry geom)
		{
			IList<ISegmentString> list = SegmentStringUtil.ExtractSegmentStrings(geom);
			return (list.Count > 0 && this.prepLine.IntersectionFinder.Intersects(list)) || (geom.Dimension != Dimension.Curve && ((geom.Dimension == Dimension.Surface && this.prepLine.IsAnyTargetComponentInTest(geom)) || (geom.Dimension == Dimension.Point && this.IsAnyTestPointInTarget(geom))));
		}

		protected bool IsAnyTestPointInTarget(IGeometry testGeom)
		{
			PointLocator pointLocator = new PointLocator();
			foreach (Coordinate p in ((IEnumerable<Coordinate>)ComponentCoordinateExtracter.GetCoordinates(testGeom)))
			{
				if (pointLocator.Intersects(p, this.prepLine.Geometry))
				{
					return true;
				}
			}
			return false;
		}

		protected PreparedLineString prepLine;
	}
}
